package fractal.filter;

public class SurfaceBlur extends FractalFilter
{
	int radius = 15;

	int drop = 2;

	int strength = 1;

	int threshold = 2;

	int iterations = 1;

	@Override
	protected void initFilter()
	{
	}

	@Override
	protected int calculatePixel(int i)
	{
		int r1, g1, b1, r2, g2, b2, Rd, Gd, Bd, dist, diff, pos = 0, offset = 0;
		r1 = (inPixels[i] & 0xff0000) >> 16;
		g1 = (inPixels[i] & 0x00ff00) >> 8;
		b1 = (inPixels[i] & 0x0000ff);
		scany : for (int y = -radius; y <= radius; y++)
		{
			offset = i + (y * width);
			if (offset < 0 || offset + radius >= inPixels.length)
				continue scany;
			scanx : for (int x = -radius; x <= radius; x++)
			{
				pos = offset + x;
				if (pos < (((pos - x) / width)) * width
						|| pos > (((pos - x) / width) + 1) * width || pos == i)
					continue scanx;
				dist = Math.max(Math.abs(x), Math.abs(y));
				dist = dist * drop;
				r2 = (inPixels[pos] & 0xff0000) >> 16;
				g2 = (inPixels[pos] & 0x00ff00) >> 8;
				b2 = (inPixels[pos] & 0x0000ff);
				Rd = Math.abs(r1 - r2);
				Gd = Math.abs(g1 - g2);
				Bd = Math.abs(b1 - b2);
				diff = Rd * Rd + Gd * Gd + Bd * Bd;
				if (diff > threshold)
					continue scanx;
				r1 = (r1 * dist + r2 * strength) / (dist + strength);
				g1 = (g1 * dist + g2 * strength) / (dist + strength);
				b1 = (b1 * dist + b2 * strength) / (dist + strength);
			}
		}
		return ((r1 & 0xff) << 16) + ((g1 & 0xff) << 8) + (b1 & 0xff);
	}
}
